#include "StdAfx.h"
#include "ResourceFactory.h"
#include "tinyxml.h"

#ifndef TEST
const char * g_xml = "<?xml version=\"1.0\" ?>"
"<Resource>"
    "<png>"
        "<it name=\"IT方块\">"
            "<p1 offset=\"0\" size=\"4182\" />"
            "<p2 offset=\"4182\" size=\"2845\" />"
            "<p3 offset=\"7027\" size=\"3081\" />"
            "<p4 offset=\"10108\" size=\"2398\" />"
            "<p5 offset=\"12506\" size=\"4157\" />"
            "<p6 offset=\"16663\" size=\"1349\" />"
            "<p7 offset=\"18012\" size=\"1028\" />"
            "<p8 offset=\"19040\" size=\"4181\" />"
            "<p9 offset=\"23221\" size=\"1741\" />"
            "<p10 offset=\"24962\" size=\"4517\" />"
            "<p11 offset=\"29479\" size=\"3871\" />"
            "<p12 offset=\"33350\" size=\"2812\" />"
            "<p13 offset=\"36162\" size=\"2695\" />"
            "<p14 offset=\"38857\" size=\"4492\" />"
            "<p15 offset=\"43349\" size=\"2145\" />"
            "<p16 offset=\"45494\" size=\"3010\" />"
            "<p17 offset=\"48504\" size=\"1625\" />"
            "<p18 offset=\"50129\" size=\"3918\" />"
            "<p19 offset=\"54047\" size=\"1517\" />"
            "<p20 offset=\"55564\" size=\"2472\" />"
            "<p21 offset=\"58036\" size=\"1784\" />"
            "<p22 offset=\"59820\" size=\"2927\" />"
            "<p23 offset=\"62747\" size=\"2509\" />"
            "<p24 offset=\"65256\" size=\"2089\" />"
            "<p25 offset=\"67345\" size=\"3029\" />"
            "<p26 offset=\"70374\" size=\"3466\" />"
            "<p27 offset=\"73840\" size=\"4200\" />"
            "<p28 offset=\"78040\" size=\"2488\" />"
            "<p29 offset=\"80528\" size=\"2870\" />"
            "<p30 offset=\"83398\" size=\"2707\" />"
            "<p31 offset=\"86105\" size=\"3796\" />"
            "<p32 offset=\"89901\" size=\"2908\" />"
            "<p33 offset=\"92809\" size=\"2727\" />"
            "<p34 offset=\"95536\" size=\"3427\" />"
            "<p35 offset=\"98963\" size=\"2642\" />"
            "<p36 offset=\"101605\" size=\"2129\" />"
            "<p37 offset=\"103734\" size=\"1608\" />"
            "<p38 offset=\"105342\" size=\"2879\" />"
            "<p39 offset=\"108221\" size=\"2582\" />"
            "<p40 offset=\"110803\" size=\"3334\" />"
            "<p41 offset=\"114137\" size=\"1178\" />"
            "<p42 offset=\"115315\" size=\"2285\" />"
            "<p43 offset=\"117600\" size=\"3948\" />"
            "<p44 offset=\"121548\" size=\"2685\" />"
            "<p45 offset=\"124233\" size=\"3808\" />"
            "<p46 offset=\"128041\" size=\"3737\" />"
            "<p47 offset=\"131778\" size=\"1649\" />"
            "<p48 offset=\"133427\" size=\"3234\" />"
            "<p49 offset=\"136661\" size=\"2947\" />"
            "<p50 offset=\"139608\" size=\"2240\" />"
            "<p51 offset=\"141848\" size=\"2360\" />"
            "<p52 offset=\"144208\" size=\"2468\" />"
            "<p53 offset=\"146676\" size=\"3513\" />"
            "<p54 offset=\"150189\" size=\"2794\" />"
        "</it>"
        "<black name=\"黑色方块\">"
            "<p55 offset=\"152983\" size=\"5894\" />"
            "<p56 offset=\"158877\" size=\"5262\" />"
            "<p57 offset=\"164139\" size=\"6120\" />"
            "<p58 offset=\"170259\" size=\"5890\" />"
            "<p59 offset=\"176149\" size=\"6980\" />"
            "<p60 offset=\"183129\" size=\"5421\" />"
            "<p61 offset=\"188550\" size=\"5669\" />"
            "<p62 offset=\"194219\" size=\"4827\" />"
            "<p63 offset=\"199046\" size=\"5841\" />"
            "<p64 offset=\"204887\" size=\"5209\" />"
            "<p65 offset=\"210096\" size=\"5487\" />"
            "<p66 offset=\"215583\" size=\"5916\" />"
            "<p67 offset=\"221499\" size=\"5414\" />"
            "<p68 offset=\"226913\" size=\"5487\" />"
            "<p69 offset=\"232400\" size=\"5556\" />"
            "<p70 offset=\"237956\" size=\"4730\" />"
            "<p71 offset=\"242686\" size=\"5097\" />"
            "<p72 offset=\"247783\" size=\"5718\" />"
            "<p73 offset=\"253501\" size=\"4932\" />"
            "<p74 offset=\"258433\" size=\"5684\" />"
            "<p75 offset=\"264117\" size=\"5819\" />"
            "<p76 offset=\"269936\" size=\"5508\" />"
            "<p77 offset=\"275444\" size=\"5694\" />"
            "<p78 offset=\"281138\" size=\"5861\" />"
            "<p79 offset=\"286999\" size=\"4980\" />"
            "<p80 offset=\"291979\" size=\"6509\" />"
            "<p81 offset=\"298488\" size=\"6091\" />"
            "<p82 offset=\"304579\" size=\"5389\" />"
            "<p83 offset=\"309968\" size=\"5179\" />"
            "<p84 offset=\"315147\" size=\"6538\" />"
            "<p85 offset=\"321685\" size=\"6271\" />"
            "<p86 offset=\"327956\" size=\"6249\" />"
            "<p87 offset=\"334205\" size=\"4966\" />"
            "<p88 offset=\"339171\" size=\"5826\" />"
            "<p89 offset=\"344997\" size=\"5826\" />"
            "<p90 offset=\"350823\" size=\"5548\" />"
            "<p91 offset=\"356371\" size=\"5928\" />"
            "<p92 offset=\"362299\" size=\"5512\" />"
            "<p93 offset=\"367811\" size=\"6655\" />"
            "<p94 offset=\"374466\" size=\"5562\" />"
            "<p95 offset=\"380028\" size=\"6068\" />"
            "<p96 offset=\"386096\" size=\"6810\" />"
            "<p97 offset=\"392906\" size=\"5132\" />"
            "<p98 offset=\"398038\" size=\"5858\" />"
            "<p99 offset=\"403896\" size=\"7593\" />"
            "<p100 offset=\"411489\" size=\"5891\" />"
            "<p101 offset=\"417380\" size=\"5880\" />"
            "<p102 offset=\"423260\" size=\"6438\" />"
            "<p103 offset=\"429698\" size=\"5501\" />"
            "<p104 offset=\"435199\" size=\"5189\" />"
            "<p105 offset=\"440388\" size=\"6068\" />"
            "<p106 offset=\"446456\" size=\"5705\" />"
            "<p107 offset=\"452161\" size=\"5501\" />"
            "<p108 offset=\"457662\" size=\"5225\" />"
            "<p109 offset=\"462887\" size=\"5856\" />"
            "<p110 offset=\"468743\" size=\"6302\" />"
            "<p111 offset=\"475045\" size=\"6262\" />"
            "<p112 offset=\"481307\" size=\"5942\" />"
            "<p113 offset=\"487249\" size=\"6040\" />"
            "<p114 offset=\"493289\" size=\"6367\" />"
            "<p115 offset=\"499656\" size=\"6098\" />"
            "<p116 offset=\"505754\" size=\"6334\" />"
            "<p117 offset=\"512088\" size=\"5888\" />"
            "<p118 offset=\"517976\" size=\"5870\" />"
            "<p119 offset=\"523846\" size=\"5556\" />"
            "<p120 offset=\"529402\" size=\"6078\" />"
        "</black>"
    "</png>"
"</Resource>"
;
#endif


ResourceFactory::ResourceFactory(void)
{
}

ResourceFactory::~ResourceFactory(void)
{
}
BOOL ResourceFactory::LoadFile(const CString&dir)
{
	return m_file.Open(dir,CFile::modeReadWrite|CFile::typeBinary);
}
BOOL ResourceFactory::GetPng(const char *category,
							 int number,
							 CArray<PngItem *> *img_list)
{
	TiXmlDocument doc;
#ifdef TEST
	if (!doc.LoadFile( "madeByHand.xml" )) {
		return FALSE;
	}
#else
	if (doc.Parse(g_xml))
	{
		return FALSE;
	}
#endif
	TiXmlElement * root = doc.RootElement();
	TiXmlHandle hRoot=TiXmlHandle(root);
	if (!root) {
		return FALSE;
	}
	TiXmlElement * png = root->FirstChildElement("png");
	if (!png) {
		return FALSE;
	}
	TiXmlElement * cate = png->FirstChildElement(category);
	if (!cate) {
		return FALSE;
	}
	int offset = 0;
	int size = 0;

	TiXmlElement *i = cate->FirstChildElement();
	
	for (i;i&&number>0;number--,i = i->NextSiblingElement()) {
		i->Attribute("offset",&offset);
		i->Attribute("size",&size);
		if (size <= 0) {
			continue;
		}
		BYTE* lpRsrc = new BYTE[size];
		m_file.Seek(offset,CFile::begin);
		if (m_file.Read(lpRsrc,size) == size) {

			PngItem *p =new PngItem;
			p->ptr = lpRsrc;
			p->size = size;
			img_list->Add(p);
			
		}
		
	}

	return TRUE;
}
BOOL ResourceFactory::GetPngCategory(CArray<CString>*list)
{
	TiXmlDocument doc;
#ifdef TEST
	if (!doc.LoadFile( "madeByHand.xml" )) {
		return FALSE;
	}
#else
	if (doc.Parse(g_xml))
	{
		return FALSE;
	}
#endif
	TiXmlElement * root = doc.RootElement();
	TiXmlHandle hRoot=TiXmlHandle(root);
	if (!root) {
		return FALSE;
	}
	TiXmlElement * png = root->FirstChildElement("png");
	if (!png) {
		return FALSE;
	}
	TiXmlElement *cate = png->FirstChildElement();

	CString name;
	for (cate;cate;cate = cate->NextSiblingElement()) {
		CStringA t = cate->Attribute("name");
		name = CA2W(t,CP_UTF8);
		list->Add(name);
	}
}